<!DOCTYPE html>
<html class="x-admin-sm">
	{include file="public/_header" /}
	<body>
		<div class="layui-card">

			<div class="layui-card-body ">
				<blockquote class="layui-elem-quote layui-quote-nm">
					<form class="layui-form layui-col-space5" onsubmit="return false;">
						<div class="layui-input-inline layui-show-xs-block">
							<input class="layui-input" lay-verify="required" placeholder="请输入标题" name="title" id="title">
						</div>
						<div class="layui-input-inline layui-show-xs-block">
							<button class="layui-btn" lay-submit="" lay-filter="sreach">查询</button>
							<button class="layui-btn layui-btn-primary" lay-submit="" lay-filter="rest">重置</button>
						</div>
					</form>
				</blockquote>
				<input type="hidden" value="{$id}" id="inputid" name="id">
				<input type="hidden" value="{$table}" id="table" name="table">
				<table class="layui-hide" id="test" lay-filter="test"></table>

				<script type="text/html" id="toolbarDemo">
					<div class="layui-btn-container">
								<button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon"></i>添加</button>
								<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="getCheckData">批量删除</button>
								
							  </div>
							</script>
				<script type="text/html" id="switchTpl">
					<input type="checkbox" name="istop" value="{{d.aid}}" lay-skin="switch" lay-text="是|否" lay-filter="sexDemo" {{ d.istop == 1 ? 'checked' : '' }}>
							</script>
				<script type="text/html" id="status">
					<input type="checkbox" name="show" value="{{d.aid}}" lay-skin="switch" lay-text="是|否" lay-filter="status" {{ d.show == 1 ? 'checked' : '' }}>
							</script>
				<script type="text/html" id="barDemo">
					<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="view">访问</a>
							  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
							  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
							</script>

			</div>
		</div>
	</body>
	<script type="text/javascript">
		$(function() {
			layui.use(['table', 'util'], function() {
				var table = layui.table;
				var util = layui.util;
				var form = layui.form;
				var id = $("#inputid").val();
				var table_name = $("#table").val();

				var tableInfo = table.render({
					elem: '#test',
					url: "{:url('admin/article/article_data')}",
					toolbar: '#toolbarDemo',
					title: '用户数据表',
					where: {
						id: id,
						table: table_name
					},
					cols: [
						[{
								type: 'checkbox',
								fixed: 'left',
								width: 80
							},
							{
								field: 'aid',
								title: 'ID',
								width: 80,
								fixed: 'left',
								unresize: true,
								sort: true
							},
							{
								field: 'title',
								title: '标题',
								width: 300,
								templet: function(d) {
									var str = '<span style="color:' + d.titlecolor + '">' + d.title + '</span>';
									return str;
								}
							},
							{
								field: 'click',
								title: '点击次数',
								sort: true,
								width: 100,
							},
							{
								field: 'inputtime',
								title: "{:lang('inputtime')}",
								sort: true,
								templet: function(d) {
									var logoSrc = util.toDateString(d.inputtime * 1000, "yyyy-MM-dd HH:mm:ss");
									return logoSrc;
								}
							}, {
								field: 'updatetime',
								title: "{:lang('updatetime')}",
								templet: function(d) {
									var logoSrc = util.toDateString(d.updatetime * 1000, "yyyy-MM-dd HH:mm:ss");
									return logoSrc;
								},
								sort: true
							},
							{
								field: 'push',
								title: '推荐',
								templet: '#switchTpl',
								unresize: true,
								width: 90,
							},
							{
								field: 'show',
								title: '显示状态',
								templet: '#status',
								unresize: true,
								width: 90,
							},
							{
								fixed: 'right',
								title: '操作',
								toolbar: '#barDemo'
							}
						]
					],
					id: 'testReload',
					page: {
						limit: 10
					}
				});

				//头工具栏事件
				table.on('toolbar(test)', function(obj) {
					var checkStatus = table.checkStatus(obj.config.id);
					switch (obj.event) {
						case 'getCheckData':
							var data1 = checkStatus.data;
							if (data1.length == 0) {
								layer.msg("你必须选择一项");
								return false;
							}
							layer.confirm('确认要删除这些文章么?', {
								icon: 3,
								title: '提示'
							}, function(index) {
								layer.close(index);
								var data = checkStatus.data;
								var str = "";
								data.forEach(function(item, index) {
									str += item.aid + ','
								})
								$.ajax({
									type: "post",
									url: "{:url('admin/article/delete_status')}",
									data: {
										aid: str,
										value: 1,
										model_id: {$Request.get.model_id},
										table: $("#table").val()
									},
									dataType: "json",
									success: function(res) {
										if (res.data) {
											layer.msg('删除成功', {
												icon: 1,
												time: 500 //2秒关闭（如果不配置，默认是3秒）
											}, function() {
												tableInfo.reload();
											});

										} else {
											layer.msg('修改失败', {
												icon: 2,
												time: 4000 //2秒关闭（如果不配置，默认是3秒）
											}, function() {
												tableInfo.reload();
											});
										}
									},
								});
							}, function(index) {

							});

							break;
						case 'add':
							var w = top.layerFrameConfig.w
							var h = top.layerFrameConfig.h
							var inIndex = parent.parent.layer.open({
								title: "{:lang('add')}",
								type: 2,
								area: [w + 'px', h + 'px'],
								content: "{:url('admin/article/fields_list',['id'=>$Request.get.id,'model_id'=>$Request.get.model_id])}",
								success: function() {
									top.layerFrameConfig.iframeB = {
										info: tableInfo,
										index: inIndex
									}
								}
							});
							break;
						case 'isAll':
							layer.msg(checkStatus.isAll ? '全选' : '未全选');
							break;
					};
				});

				//监听行工具事件
				table.on('tool(test)', function(obj) {
					var data = obj.data;
					if (obj.event === 'del') {
						layer.confirm('真的删除行么', function(index) {
							$.ajax({
								type: "post",
								url: "{:url('admin/article/delete_status')}",
								data: {
									aid: data.aid,
									table: $("#table").val()
								},
								dataType: "json",
								success: function(res) {
									if (res.data) {
										layer.msg('删除成功', {
											icon: 1,
											time: 500 //2秒关闭（如果不配置，默认是3秒）
										}, function() {
											obj.del();
											layer.close(index);
											tableInfo.reload();
										});

									} else {
										layer.msg('删除失败', {
											icon: 2,
											time: 4000 //2秒关闭（如果不配置，默认是3秒）
										}, function() {
											tableInfo.reload();
										});
									}
								},
							});

						});
					} else if (obj.event === 'edit') {
						var w = top.layerFrameConfig.w
						var h = top.layerFrameConfig.h
						var inIndex = parent.parent.layer.open({
							title: "{:lang('edit')}",
							type: 2,
							maxmin: true,
							area: [w + 'px', h + 'px'],
							content: "{:url("admin/article/art_edit")}?cid=" + obj.data.cid + "&aid=" + obj.data.aid + "&model_id=" + {$model['id']},
							success: function() {
								top.layerFrameConfig.iframeB = {
									info: tableInfo,
									index: inIndex
								}
							}
						});
					} else if (obj.event === 'view') {
						var url = "{$model.route}";
						window.open("/index.php/" + url + ".html?aid=" + data.aid + "&cid=" + data.cid)
					}
				});
				form.on('switch(sexDemo)', function(obj) {
					$.ajax({
						type: "get",
						url: "{:url('admin/Article/push')}",
						data: {
							table: $("#table").val(),
							aid: this.value,
							value: obj.elem.checked ? 1 : 0
						},
						dataType: "json",
						success: function(res) {
							if (res.data) {
								layer.msg('修改成功', {
									icon: 1,
									time: 2000 //2秒关闭（如果不配置，默认是3秒）
								});

							} else {
								layer.msg('修改失败', {
									icon: 2,
									time: 4000 //2秒关闭（如果不配置，默认是3秒）
								}, function() {
									tableInfo.reload();
								});
							}
						},
					});
				});

				form.on('switch(status)', function(obj) {
					$.ajax({
						type: "get",
						url: "{:url('admin/Article/show')}",
						data: {
							table: $("#table").val(),
							aid: this.value,
							value: obj.elem.checked ? 1 : 0
						},
						dataType: "json",
						success: function(res) {
							if (res.data) {
								layer.msg('修改成功', {
									icon: 1,
									time: 2000 //2秒关闭（如果不配置，默认是3秒）
								});

							} else {
								layer.msg('修改失败', {
									icon: 2,
									time: 4000 //2秒关闭（如果不配置，默认是3秒）
								}, function() {
									tableInfo.reload();
								});
							}
						},
					});

				});



				form.on('submit(sreach)', function(data) {
					var id = $("#inputid").val();
					var table_name = $("#table").val();
					//执行重载
					      table.reload('testReload', {
					        page: {
					          curr: 1 //重新从第 1 页开始
					        },
					        where: {
					          id: id,
							  title:data.field.title,
					          table: table_name
					        }
					      });
					return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
				});
				form.on('submit(rest)', function(data) {
					table.reload('testReload', {
					  page: {
					    curr: 1 //重新从第 1 页开始
					  },
					  where: {
					    id: id,
						title:"",
					    table: table_name
					  }
					});
					$("#title").val("");
					return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
				});


			});


		})
	</script>
</html>
